package com.chenfan.ccp.plug.rpc.service.impl;

import com.chenfan.infra.constants.CommonBeanConstants;
import com.chenfan.ccp.util.start.ApplicationContextUtil;
import com.chenfan.ccp.util.tool.HttpUtil;
import com.chenfan.ccp.util.tool.JsonUtil;
import com.chenfan.ccp.plug.rpc.service.ApprovalRemoteService;
import org.springframework.beans.factory.annotation.Value;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author K
 * @date 2021/1/25
 */
public class DefaultApprovalRemoteServiceImpl implements ApprovalRemoteService {

    public static final String SEND_APPROVAL_URL = "/approval/";

    @Value("${paas.approval.url}")
    private String url;

    @Override
    public Long startProcess(Long approvalTemplateId, Long useId, String userName, String param) {
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("projectName", ApplicationContextUtil.getServiceName());
        paramMap.put("useId", useId);
        paramMap.put("userName", userName);
        if (Objects.nonNull(param)) {
            Map<String, Object> params = new HashMap<>(4);
            params.put("parameter", param);
            paramMap.put("param", params);
        }
        String post = HttpUtil.post(url + SEND_APPROVAL_URL + approvalTemplateId, JsonUtil.obj2String(paramMap));
        Map<String, String> map = JsonUtil.string2Obj(post, Map.class);
        return Long.parseLong(map.getOrDefault("obj", "0"));
    }

    @Override
    public Long revokeApproval(Long approvalInstanceId) {
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("projectName", ApplicationContextUtil.getServiceName());
        String post = HttpUtil.post(url + SEND_APPROVAL_URL + approvalInstanceId + "/revoke", JsonUtil.obj2String(paramMap));
        Map<String, String> map = JsonUtil.string2Obj(post, Map.class);
        return Long.parseLong(map.getOrDefault("obj", "0"));
    }

    @Override
    public Long revokeApproval(Long approvalInstanceId, Long userId, String userName) {
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("projectName", ApplicationContextUtil.getServiceName());
        paramMap.put(CommonBeanConstants.USER_ID, userId);
        paramMap.put("userName", userName);
        String post = HttpUtil.post(url + SEND_APPROVAL_URL + approvalInstanceId + "/revoke", JsonUtil.obj2String(paramMap));
        Map<String, String> map = JsonUtil.string2Obj(post, Map.class);
        return Long.parseLong(map.getOrDefault("obj", "0"));
    }

    @Override
    public String getInstanceStatusList(Long approvalTemplateId, Long approvalUserId, Integer pageNum, Integer pageSize) {
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("projectName", ApplicationContextUtil.getServiceName());
        if (Objects.nonNull(approvalUserId)) {
            paramMap.put(CommonBeanConstants.USER_ID, approvalUserId);
        }
        if (Objects.nonNull(approvalTemplateId)) {
            paramMap.put("approvalTemplateId", approvalTemplateId);
        }
        paramMap.put("pageNum", pageNum);
        paramMap.put("pageSize", pageSize);
        return HttpUtil.get(url + SEND_APPROVAL_URL + "list", paramMap);
    }

    @Override
    public String approvalToMap(Collection<Long> approvalIdList) {
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("projectName", ApplicationContextUtil.getServiceName());
        if (Objects.nonNull(approvalIdList)) {
            paramMap.put("approvalIdList", approvalIdList);
        }
        return HttpUtil.post(url + SEND_APPROVAL_URL + "processing", JsonUtil.obj2String(paramMap));
    }
}
